<?php

namespace backend\controllers;

class RoleController extends \yii\web\Controller
{
    public function actionIndex()
    {
        var_dump($this->getControllers());
        die();
        return $this->render('index');
    }

    public function getActions($controller)
    {

        $path = 'frontend' . DIRECTORY_SEPARATOR . 'controllers' . DIRECTORY_SEPARATOR;
        $reflection = new \ReflectionClass($path . $controller);
        $methods = $reflection->getMethods();

        //$cInstance=new $controller(null);
        // var_dump($cInstance->actions());
        $actions = array();
        foreach ($methods as $method) {
            if (strpos($method->name, 'action') === 0 and ctype_upper($method->name[6])) {
                $actions[] = str_replace('action', '', $method->name);
            }
        }
        return $actions;

    }

    /**
     * Get list of controllers with actions
     *
     * @param mixed $module
     * @return array
     */
    function getControllersActions()
    {
        $c = $this->getControllers();
        foreach ($c as &$controller) {
            $controller = array(
                'name' => $controller,
                'actions' => $this->getActions($controller)
            );
        }
        return $c;
    }

    /**
     * Scans controller directory & return array of MVC controllers
     *
     * @param mixed $module
     * @param mixed $include_classes
     * @return array
     */
    public function getControllers()
    {

        $appControllerPath = Yii::$app->basePath . DIRECTORY_SEPARATOR . 'controllers';
        //checking existence of controllers directory
        if (is_dir($appControllerPath))
            $fileLists = FileHelper::findFiles($appControllerPath);
        $controllers = array();
        foreach ($fileLists as $controllerPath) {
            //getting controller name like e.g. 'siteController.php'
            $controllers[] = substr($controllerPath, strrpos($controllerPath, DIRECTORY_SEPARATOR) + 1, -4);
        }
        return $controllers;
    }

    /**
     * Used in getModules() to filter array of files & directories
     *
     * @param mixed $a
     */
    private function isController($a)
    {
        return stripos($a, 'Controller.php') !== false;
    }

}
